<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/11/5
 * Time: 上午12:38
 */

namespace App\Repositories\Addons;

use Illuminate\View\View;

class HtmlRepository extends AddonsRepository
{
    private static $instance;

    /**
     * 实现单例
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * 百度编辑器js调用次数
     * @var int
     */
    private static $uEditorStatic = 0;

    /**
     * 调用编辑器
     * @param $inputName
     * @param string $inputValue
     * @return mixed
     */
    public function editor($inputName, $inputValue = '')
    {
        self::$uEditorStatic++;

        $content =  view('addons.editor', ['inputName'=>$inputName, 'inputValue'=>$inputValue, 'uEditorStatic'=>self::$uEditorStatic]);

        return response($content)->getContent();
    }

    /**
     * 图片上传
     * @param $inputName
     * @param array $inputValue [ ['id'=>0, 'url'=>'', 'desc'=>''] ]
     * @param int $maxNumber 上传数量 0 无限制
     * @return \Illuminate\Contracts\View\Factory|View
     */
    public function uploadPicture($inputName, array $inputValue = [], $maxNumber = 1)
    {
        static $static = 0;
        $static++;

        self::$uEditorStatic++;

        $content =  view('addons.upload_picture', ['inputName'=>$inputName, 'inputValue'=>$inputValue, 'uEditorStatic'=>self::$uEditorStatic, 'maxNumber'=>$maxNumber, 'static'=>$static]);

        return response($content)->getContent();
    }

    /**
     * 文件上传
     * @param $inputName
     * @param array $inputValue [ ['id'=>0, 'url'=>'', 'desc'=>''] ]
     * @param int $maxNumber 上传数量 0 无限制
     * @return \Illuminate\Contracts\View\Factory|View
     */
    public function uploadFile($inputName, array $inputValue = [], $maxNumber = 1)
    {
        static $static = 0;
        $static++;

        self::$uEditorStatic++;

        $content =  view('addons.upload_file', ['inputName'=>$inputName, 'inputValue'=>$inputValue, 'uEditorStatic'=>self::$uEditorStatic, 'maxNumber'=>$maxNumber, 'static'=>$static]);

        return response($content)->getContent();
    }

    /**
     * 下拉选项框
     * @param array $list
     * @param $inputName
     * @param string $inputValue
     * @param array $config
     * @return string
     */
    public function select(array $list, $inputName, $inputValue = null, array $config = [])
    {
        $content = view('addons.select', ['list'=>$list, 'inputName'=>$inputName, 'inputValue'=>$inputValue, 'config'=>$config]);

        return response($content)->getContent();
    }

    /**
     * 单选框
     * @param array $list
     * @param $inputName
     * @param null $inputValue
     * @param array $config
     * @return string
     */
    public function radio(array $list, $inputName, $inputValue = null, array $config = [])
    {
        $inputValue = is_array($inputValue) ? $inputValue : explode(',', $inputValue);

        $content = view('addons.radio', ['list'=>$list, 'inputName'=>$inputName, 'inputValue'=>$inputValue, 'config'=>$config]);

        return response($content)->getContent();
    }

    /**
     * 多选框
     * @param array $list
     * @param $inputName
     * @param null $inputValue
     * @param array $config
     * @return string
     */
    public function checkbox(array $list, $inputName, $inputValue = null, array $config = [])
    {
        $inputValue = is_array($inputValue) ? $inputValue : explode(',', $inputValue);

        $content = view('addons.checkbox', ['list'=>$list, 'inputName'=>$inputName, 'inputValue'=>$inputValue, 'config'=>$config]);

        return response($content)->getContent();
    }

    /**
     * textarea 框
     * @param $inputName
     * @param null $inputValue
     * @param array $config
     * @return string
     */
    public function textarea($inputName, $inputValue = null, array $config = [])
    {
        $content = view('addons.textarea', ['inputName'=>$inputName, 'inputValue'=>$inputValue, 'config'=>$config]);

        return response($content)->getContent();
    }

    /**
     * input框
     * @param $inputName
     * @param null $inputValue
     * @param array $config
     * @return string
     */
    public function input($inputName, $inputValue = null, array $config = [])
    {
        $content = view('addons.input', ['inputName'=>$inputName, 'inputValue'=>$inputValue, 'config'=>$config]);

        return response($content)->getContent();
    }
}